/**
 * Created by LT on 2016/3/10.
 */

var COMMON = COMMON || {};
COMMON.enableBlur=true;
COMMON.SUCCESS = "SUCCESS";
COMMON.FAILED = "FAILED";
COMMON.openFormWin = function (id, callback, opt) {
    var win = $("#" + id).window($.extend({
        top: 50,
        left: 100,
        onOpen:function() {
            COMMON.switchEnableBlur($(this).parent().find(".panel-tool"));
        }
    }, opt || {}));
    callback && $.isFunction(callback) && callback();
    win.slideDown();
    win.window("open");
};

COMMON.initForm = function (id, url, callback) {
    return $('#' + id).form({
        url: url,
        onSubmit: function () {
            $.messager.progress({
                interval: 100,
                text: "正在保存..."
            });
            var isValid = $(this).form('validate'),
                $tbs = $(".easyui-textbox", this);
            if (!isValid) {
               /* $("#crsTypes").combobox('textbox').trigger('mouseover');
                $("#crsNames").combobox('textbox').trigger('mouseover');*/
                $.messager.progress('close');
            }
                $tbs.each(function () {
                $(this).textbox("setValue", $.trim($(this).textbox("getValue")));
            })
            return isValid;
        },
        success: function (result) {
            $.messager.progress('close');
            callback && $.isFunction(callback) && callback(IPEBG.str2json(result));
        }
    });
};

COMMON.del = function (url, data, callback) {
    $.messager.confirm("系統提示", "你確定要刪除嗎？", function (r) {
        if (r) {
            $.post(url, data, function (result) {
                $.messager.progress({
                    interval: 100,
                    text: "正在刪除..."
                });
                callback && $.isFunction(callback) && callback(result);
                $.messager.progress('close');
            })
        }
    });
};

COMMON.initTable = function (id, opt) {
    return $("#" + id).datagrid($.extend({
        title: '',
        nowrap: false,
        striped: true,
        scrollbarSize: 0,
        fitColumns: true,
        remoteSort: false,
        singleSelect: true,
        rownumbers: true,
        frozenColumns: [[
            {field: 'ck', checkbox: true, width: 30}
        ]],
        pagination: true,
        pageSize: 15,
        pageList: [15, 30, 45]
    }, opt || {}));
};

COMMON.dateFormatter = function (value) {
    return new Date(value - 0).format("yyyy/MM/dd");
};

COMMON.getMhsEmp = function (ctx, empNo, success, fail) {
    $.get(ctx + "/mhs/getEmp.do?empNo=" + empNo, function (result) {
        if (result === "FAILED") {
            $.messager.alert('系統提示', '工號不存在！', 'info');
            fail && $.isFunction(fail) && fail();
            return;
        }
        success && $.isFunction(success) && success(IPEBG.str2json(result));
    });
};

COMMON.getButtons=function(ctx,functionId,callback) {
    $.get(ctx + "/common.do?action=getButtons&functionId=" + functionId, function (data) {
        callback && $.isFunction(callback) && callback(data);
    });
};

COMMON.mouseIsInRange = function (selector, top, left) {
    var $dom = $(selector),
        rOffset = $dom.offset(),
        rH = $dom.height(),
        rW = $dom.width();
    return rOffset.top <= top && rOffset.left <= left && rOffset.top + rH >= top && rOffset.left + rW > left;
};

COMMON.switchEnableBlur=function($dom) {
    $dom.on("mouseleave",function() {
        COMMON.enableBlur = true;
    }).on("mouseenter",function() {
        COMMON.enableBlur = false;
    });
};

COMMON._procAjaxParam=function(){
    var args = [].slice.apply(arguments),
        callback=function(){},
        callbackWrapper = function(data){
            data = IPEBG.str2json(data);
            callback(data);
        };
    switch (args.length) {
        case 2:
            callback = args[1];
            args[1] = callbackWrapper;
            break;
        case 3:
            callback = args[2];
            args[2] = callbackWrapper;
            break;
    }
    return args;
}

COMMON.post=function() {
    $.post.apply($, COMMON._procAjaxParam.apply(this,arguments));
};

COMMON.get=function(){
    $.get.apply($, COMMON._procAjaxParam.apply(this,arguments));
};

COMMON.alert=function(data) {
    $.messager.alert('系統提示', "["+data.code+"]"+data.msg, 'info');
};

$(function () {
    COMMON.getButtons(ctx, IPEBG.getQueryString("id"), function (data) {
    	var jData=IPEBG.str2json(data);
    	$.each(jData, function (i, o) {
            $(".easyui-linkbutton[code='" + o.optionId + "']").show();
        });
    });
});